package vn.hanelsoft.openwifi.control;

import vn.hanelsoft.openwifi.common.ConstCommon;
import vn.hanelsoft.openwifi.common.UtilsCommon;
import vn.hanelsoft.openwifi.model.delegate.ServerDelegate;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class KillService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		final Context mContext = this;
		// while (ConstCommon.totalTimeService >=0) {
		// try {
		// Thread.sleep(1000);
		// ConstCommon.totalTimeService ++;
		// Log.d("Luong",
		// "------------------------ time: "+ConstCommon.totalTimeService);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// if(ConstCommon.totalTimeService >=100)
		// {
		// ConstCommon.totalTimeService = -1;
		// UtilsCommon.killApp(mContext);
		// }
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				while (ConstCommon.totalTimeService > 0) {
//					try {
//						Thread.sleep(1000);
//						ConstCommon.totalTimeService++;
//						Log.d("Luong", "------------------------ time: "
//								+ ConstCommon.totalTimeService);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					if (ConstCommon.totalTimeService >= 100) {
//						ConstCommon.totalTimeService = -1;
//						UtilsCommon.killApp(mContext);
//					}
//				}
//			}
//		}).start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					while (ConstCommon.isServiceRunning && (ConstCommon.minuterService > 0
							|| ConstCommon.secondService > 0)) {
						if (ConstCommon.secondService > 0)
							ConstCommon.secondService--;
						else if (ConstCommon.minuterService > 0) {
							ConstCommon.minuterService--;
							ConstCommon.secondService = 59;
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Log.i("Service1", "Waiting " + ConstCommon.minuterService + ":" + ConstCommon.secondService);
					}
					if (ConstCommon.isServiceRunning) {
						if(ConstCommon.isServiceRunning){
							if (ConstCommon.isFirst){
								ConstCommon.totalSecondService = (ConstCommon.minuterFirst * 60 - (ConstCommon.minuterService * 60 + ConstCommon.secondService));
								ConstCommon.isFirst = false;
							}
							else
								ConstCommon.totalSecondService = (ConstCommon.minuterWait*60 - (ConstCommon.minuterService*60+ConstCommon.secondService));
							ConstCommon.minuterService = ConstCommon.minuterWait;
							ConstCommon.secondService = 0;
						}
						Log.i("Service1", "totalSecondService " + ConstCommon.totalSecondService);
						ServerDelegate.updateTotalTimeShowAd(mhandler, UtilsCommon
								.getStringSetting(getBaseContext(),
										ConstCommon.KEY_WIFI_STORE), String
								.valueOf(ConstCommon.totalSecondService));
						ConstCommon.totalSecondService = 0;
						ConstCommon.isServiceRunning = false;
						UtilsCommon.killApp(getBaseContext());
						Log.i("Service1", "Kill");
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.i("Service1", "Waiting");
				}
			}
		}).start();
		/*
		 * try { new Thread(new Runnable() {
		 * 
		 * @Override public void run() { try { Thread.sleep(1000);
		 * ConstCommon.totalTimeService ++; Log.d("Luong",
		 * "------------------------ time: "+ConstCommon.totalTimeService); }
		 * catch (InterruptedException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } if(ConstCommon.totalTimeService >=100) {
		 * ConstCommon.totalTimeService = -1; UtilsCommon.killApp(mContext); } }
		 * }).start(); } catch (Exception e) { // TODO: handle exception }
		 */

		// }
		return startId;
	}
	
	Handler mhandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.arg1 == 0) {
				UtilsCommon.killApp(getApplicationContext());
			}
		}
	};

	@Override
	public void onDestroy() {
		// Log.d("Luong",
		// "----------Destroy service: "+ConstCommon.totalTimeService);
		ConstCommon.totalTimeService = -1;
		super.onDestroy();
	}
}
